// pages/userMess/userMess.js
var api = require('../../utils/api.js')
const config = require(`../../config.js`);
const { async } = require('../../utils/runtime/runtime.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: '',
    city: '',
    province:'',
    avatarUrl:'',
    gender: '',
    nickName:'',
    referee:'',
    cityId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.cityId){
      wx.setStorageSync("cityId", options.cityId)
    }
    this.setData({
      types: options.types,
      cityId: options.cityId,
      referee: options.userId,
    })
  },


  getUserMess(res) {
   
    let that=this
    if (res.detail.errMsg == 'getUserInfo:ok') {
      wx.setStorageSync('userMessage', res.detail.userInfo)
      this.setData({
        city: res.detail.userInfo.city, 
        province: res.detail.userInfo.province, 
        avatarUrl: res.detail.userInfo.avatarUrl, 
        gender: res.detail.userInfo.gender, 
        nickName: res.detail.userInfo.nickName,
        userMessage: res.detail.userInfo
      })
      wx.login({
        success: rest => {
          wx.request({
            url: `${config.url}/admin/user/checkUser`,
            data: {
              wxCode: rest.code,

            },
            header: {
              'content-type': 'application/json',
            },
            method: 'POST',
            success: function(res) {
              if (res.data.result == true) {
                wx.login({
                  success: rest => {
                    wx.request({
                      url: `${config.url}/admin/user/login`,
                      data: {
                        wxCode: rest.code,
                        loginType: '1',
                      },
                      header: {
                        'content-type': 'application/json',
                      },
                      method: 'POST',
                      success: async function(res) {
                        console.log(res)
                        if (res.data.result == true) {
                          // if (wx.getStorageSync("userId") != "") {
                           
                          // }
                          if (res.data.code==99){
                            //本地存储
                            await wx.setStorageSync("token", res.data.data)
                            //城市Id
                            //codeNum
                            await wx.setStorageSync("codeNum", res.data.code)
                            wx.setStorageSync("userMess", res.data.object)
                            await wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                            wx.setStorageSync("phone", res.data.object.phone)
                            await wx.setStorageSync("openId", res.data.body.openid)
                            //0是别人登陆 1是商家登陆 2个人登陆
                            wx.setStorageSync("type", res.data.object.type)
                            that.empower()
                            wx.reLaunch({
                              url: '../index/index',
                            })
                          }
                        }else{
                          if (res.data.code == 103||res.data.code == 105) {
                            //本地存储
                            wx.setStorageSync("token", res.data.data)
                            wx.setStorageSync("phone", res.data.object.phone)
                            wx.setStorageSync("openId", res.data.body.openid)
                            //城市Id
                            //codeNum
                            wx.setStorageSync("codeNum", res.data.code)
                            await wx.setStorageSync("userMess", res.data.object)
                            wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                            wx.navigateTo({
                          
                              url: '../type/type',
                            })
                          }
                          else if (res.data.code == 104) {
                            //本地存储
                            await wx.setStorageSync("token", res.data.data)
                            wx.setStorageSync("phone", res.data.object.phone)
                            wx.setStorageSync("openId", res.data.body.openid)
                            //城市Id
                            //codeNum
                            wx.setStorageSync("codeNum", res.data.code)
                            wx.setStorageSync("userMess", res.data.object)
                            wx.setStorageSync("vipFlag", res.data.object.vipFlag)
                            wx.navigateTo({

                              url: '../shopBack/shopBack',
                            })
                          }
                        } 
                      }
                    })
                 
                  }
                })
              } else {
            
                wx.navigateTo({
                  url: '../login/login?city=' + that.data.city + '&province=' + that.data.province + '&avatarUrl=' +
                    that.data.avatarUrl + '&gender=' + that.data.gender + '&nickName=' + that.data.nickName + '&cityId=' + that.data.cityId + '&referee=' + that.data.referee,
                })
              }
            }
          })

        }
      })


    } else {
      wx.navigateBack({
        delta: 0
      })
    }
  },

  empower: function (res) {
   
    api.empower({
      grantUserId: wx.getStorageSync("userId"),
      userId: wx.getStorageSync("userMess").id,
    }).then(res => {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (this.data.types == 2) {
      wx.switchTab({
        url: '../index/index'
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})